<?php 
use Wdl\helper\webHelper;
 
 defined('IN_IA') or exit('Access Denied');
 global $_GPC, $_W;
 load()->func('tpl'); 
 $uniacid = webHelper::bindUniacid(); 
 $openid = $_W['openid'];
 $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'index';	
 $template_name = sprintf("news/%s",  $operation);
 
 $rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';	

 $categorys = [0=> '通知公告', 1=> '业务资讯'];

 if($operation =='add'){

    $id = intval($_GPC['id']);

    if (checksubmit('submit')) {
        $data = $_GPC['data'];
        $data['thumb'] = $_GPC['thumb'];
        $data['newstext'] = trim($_GPC['newstext']);
        if (empty($data['title'])) {
            message('抱歉，请输入标题！');
        }
        /*栏目信息*/
        $data['fname'] =  isset($categorys[$data['fid']]) ? $categorys[$data['fid']] : '';
       
        if (!empty($id)) {
            pdo_update('kbwdl_saleinfo', $data, array('id' => $id, 'uniacid' => $uniacid));
           
        } else {
            $data['uniacid'] = $uniacid ;
            $data['addtime'] =  date('Y-m-d H:i:s', TIMESTAMP);
            $data['tid'] = 10; /**数据分类 活动*/
            if(empty($data['onclick'])){
                $data['onclick'] = 1;
            }
            pdo_insert('kbwdl_saleinfo', $data);
            $id = pdo_insertid();
        }
        message('操作成功', $this->createWebUrl('news', array('op' => 'index')), 'success');
    }
    
    if (!empty($id)) {
        $info = pdo_fetch("SELECT * FROM " . tablename('kbwdl_saleinfo') 
                . " WHERE tid=10 and id = :id AND uniacid = :uniacid", 
                array(':id' => $id, ':uniacid' => $uniacid ));
        $info['newstext'] = htmlspecialchars_decode($info['newstext']);
        
    } 
 }elseif($operation == 'index'){
    
    if (!empty($_GPC['displayorder'])) {
        foreach ($_GPC['displayorder'] as $id => $displayorder) {
            pdo_update('kbwdl_saleinfo', array('orderid' => $displayorder), array('id' => $id, 'uniacid' => $uniacid ));
        }
        message('排序更新成功！', $this->createWebUrl('news', array('op' => 'index')), 'success');
    }
    $condition = " WHERE uniacid = :uniacid and tid=:tid ";
    $params = array('uniacid' => $uniacid , 'tid'=>10);

    $ret = webHelper::table_list_data('kbwdl_saleinfo',$condition,$params, "orderid desc, id desc" );
    $total = $ret['total'];
    $list = $ret['list'];
    $pager = $ret['pager'];

 }elseif($operation == 'delete'){

    $id = intval($_GPC['id']);
    $category = pdo_fetch("SELECT id,title FROM " . tablename('kbwdl_saleinfo') 
        . " WHERE id = :id and tid=10 AND uniacid =:uniacid ",['id' => $id, 'uniacid' => $uniacid ]);
    if (empty($category)) {
        message('抱歉，不存在或是已经被删除！', $this->createWebUrl('news', array('op' => 'index')), 'error');
    }
    pdo_delete('kbwdl_saleinfo', array('id' => $id, 'uniacid' => $uniacid ));
    message('删除成功！', $this->createWebUrl('news', array('op' => 'index')), 'success');
    exit;
    
 }
 //下面是公共数据
    $submenus = [
        'index'=>['name'=> '管理资讯'],
        'add'=>['name'=> '发布资讯'],
    ];
 include $this->template('news/layout');